package cn.lijigou.shopping.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.test.AndroidTestCase;
import android.util.Log;
import android.widget.Toast;
import cn.lijigou.shopping.LoginActivity;
import cn.lijigou.shopping.ShoppingsActivity;
import cn.lijigou.shopping.domain.Advertising;
import cn.lijigou.shopping.domain.CityTag;
import cn.lijigou.shopping.domain.MLocation;
import cn.lijigou.shopping.domain.MResult;
import cn.lijigou.shopping.domain.Order;
import cn.lijigou.shopping.domain.Shop;
import cn.lijigou.shopping.domain.User;
import cn.lijigou.shopping.domain.Website;

public class MSystem extends AndroidTestCase {
	public static int pv = 0;				//PV 统计
	public static int buy = 0;				//点购买按钮  统计
	public static int detail  = 0;			//点团购详情  统计
	public static int last_start_time = 0;	//开机开始时间
	public static int last_end_time = 0;	//开机结束时间
	public static int edit = 0;				//定位修改统计
	
	public static MLocation mLocation;
	public static String city = "";
	public static String selectCity = "";
	public static String area = "";
	public static String areaName = "";
	public static String lat = "";
	public static String lng = "";
	public static String uid = "";
	public static String session = "";
	public static String nickname = "";
	public static String isTmp = "";
	public static String lat_temp = "";
	public static String lng_temp = "";
	public static TimerTask task = null;
	public static Timer timer_shoppings;
	public static boolean ok_shoppings = true;
	public static SharedPreferences mSharedPreference = null;

	public static void test() {
		List<Website> websites = new ArrayList<Website>();
		getHotWebsite(websites);
	}

	public MSystem() {
		super();
	}

	public MSystem(ShoppingsActivity shoppings) {
		timer_location_shoppings(shoppings);
	}
	/**
	 * 开机统计
	 * @param context
	 * @return
	 */
	public static MResult open(Context context) {
		return ServerService.open(context);
	}
	/**
	 * 定位统计
	 * @param lat
	 * @param lng
	 * @param city
	 * @param address
	 * @return
	 */
	public static MResult location_stat(String lat,String lng,String city,String address) {
		return ServerService.location_stat(lat,lng,city,address);
	}

	public void timer_location_shoppings(final ShoppingsActivity shoppings) {
		if (task != null) {
			task.cancel();
			ok_shoppings = true;
		}
		task = new TimerTask() {
			public void run() {
				if (ok_shoppings) {
					ok_shoppings = false;
					if (mLocation != null) {
						timer_shoppings.cancel();
						lat = String.valueOf(mLocation.getLat());
						lng = String.valueOf(mLocation.getLng());
						lat_temp = lat;
						lng_temp = lng;
						shoppings.waitHander
								.sendEmptyMessage(shoppings.SET_SHOPPINGS);
					} else {
						ok_shoppings = true;
					}
				}
			}
		};
		if (timer_shoppings != null) {
			timer_shoppings.cancel();
		}
		timer_shoppings = new Timer();
		timer_shoppings.schedule(task, 0, 1000);
	}

	/**
	 * 判断用户是否登录
	 * 
	 * @param activity
	 * @param
	 * @return
	 */
	public static boolean isLogin(Activity activity, int type) {
		if (mSharedPreference == null) {
			mSharedPreference = activity.getSharedPreferences("user",
					Context.MODE_PRIVATE);
		}
		uid = mSharedPreference.getString("uid", "");
		session = mSharedPreference.getString("session", "");
		nickname = mSharedPreference.getString("nickname", "");

		if (!"".endsWith(uid) || !"".endsWith(session)) {
			return true;
		} else if (type == 1) {
			Toast.makeText(activity, "嗨，快去登录吧", 1).show();
			Intent intent = new Intent(activity, LoginActivity.class);
			activity.startActivity(intent);
		}
		return false;
	}

	/**
	 * 判断用户是否为虚拟用户登录 true 代表虚拟账户
	 * 
	 * @param activity
	 * @return
	 */
	public static boolean isLoginDummy(Activity activity) {
		if (mSharedPreference == null) {
			mSharedPreference = activity.getSharedPreferences("user",
					Context.MODE_PRIVATE);
		}
		uid = mSharedPreference.getString("uid", "");
		session = mSharedPreference.getString("session", "");
		nickname = mSharedPreference.getString("nickname", "");
		isTmp = mSharedPreference.getString("isTmp", "1");

		if ("1".equals(isTmp)) {
			return true;
		}
		return false;
	}

	/**
	 * 用户登录
	 * 
	 * @param input_string
	 * @param password
	 * @return
	 */
	public static MResult Login(User user, String name, String password) {
		return ServerService.login(user, name, password, session, uid);
	}

	/**
	 * 用户注册
	 * 
	 * @param mAccount
	 * @param mPasswd1
	 * @return
	 */
	public static MResult Register(User user, String name, String mPasswd1) {
		return ServerService.register(user, name, mPasswd1, session, uid);
	}
	/**
	 * 用户登录|注册(weibo)
	 * @param user 要登录|注册的用户
	 * @param oauth_token 用户签名
	 * @param oauth_token_secret 用户密钥
	 * @param source QQ/Sina/RenRen
	 * @return MResult= null 网络异常或者解析数据出错
	 */
	public static MResult login_register_weibo(User user,String oauth_token, String oauth_token_secret,String source){
		return ServerService.register_weibo(user,oauth_token, oauth_token_secret,source,session,uid);
	}
	/**
	 * 搜索团购
	 * 
	 * @param shops
	 * @param page
	 * @param keyword
	 * @return
	 */
	public static MResult searchShops(List<Shop> shops, int page, String keyword) {
		if (mLocation != null) {
			lat = String.valueOf(mLocation.getLat());
			lng = String.valueOf(mLocation.getLng());
		}
		if ("".equals(selectCity)) {// 没有选中城市 ，city为自动获取城市
			city = mLocation.getCity();
		} else {
			city = selectCity;
		}
		return ServerService.searchShops(shops, lat, lng, page, keyword, city);
	}

	/**
	 * 获得附近团购
	 * @param cat 分类
	 * @param distance 距离
	 * @param area 商圈
	 * @param city 城市
	 * @param shops 团购集合
	 * @param sort  排序方法默认为0 按照距离。sort=1按照价格
	 * @param order 排序类型 当为sort=1时候有效。默认为1 正序（由小到大，由便宜到贵）2为倒序
	 */
	public static MResult getShopsFromAround(List<Shop> shops, int page, String cat,String area, String distance,int sort, int order) {
		if ("".equals(selectCity)) {// 没有选中城市 ，city为自动获取城市
			city = mLocation.getCity();
		} else {
			city = selectCity;
		}
		
		return ServerService.getShopsFromAround(shops, lat, lng, page, cat, area, city , distance,sort,order);
	}
	/**
	 * 获得今日团购
	 * @param shops 团购集合
	 * @param page  页码
	 * @param sort 	1 为默认人气    2  为分类(1美食天下,2休闲娱乐,3网购精品,4生活服务，7酒店旅游，默认为1) 3 为价格
	 * @param order 排序方法：1正序，2倒序
	 * @param cat 	sort=2 的分类
	 * @return
	 */
	public static MResult getShopsFromToday(List<Shop> shops,String cat, int page, int sort, int order) {
		if ("".equals(selectCity)&& mLocation != null) {// 没有选中城市 ，city为自动获取城市
			city = mLocation.getCity();
		} else {
			city = selectCity;
		}
		return ServerService.getShopsFromToday(shops, cat ,page, city,sort, order);
	}
	/**
	 * 获得热门网站团购
	 * @param shops	团购集合
	 * @param page	页码
	 * @param website_id  团购网站id
	 * @param sort 1 为默认人气    2  为分类(1美食天下,2休闲娱乐,3网购精品,4生活服务，7酒店旅游，默认为1) 3 为价格
	 * @param order 排序方法：1正序，2倒序
	 * @param cat 	sort=2 的分类
	 * @return
	 */
	public static MResult getShopsFromHotWebsite(List<Shop> shops,String cat,String website_id, int page,int sort, int order) {
		if ("".equals(selectCity) && mLocation != null) {// 没有选中城市 ，city为自动获取城市
			city = mLocation.getCity();
		} else {
			city = selectCity;
		}
		return ServerService.getShopsFromHotWebsite(shops,cat,website_id, page, city,sort, order);
	}

	/**
	 * 获得热门团购网站
	 * 
	 * @param city
	 *            城市
	 * @param websites
	 *            团购网站集合
	 * @return
	 */
	public static MResult getHotWebsite(List<Website> websites) {

		if ("".equals(selectCity) && mLocation != null) {// 没有选中城市 ，city为自动获取城市
			city = mLocation.getCity();
		} else {
			city = selectCity;
		}
		return ServerService.getHotWebsite(websites, city);
	}
	/**
	 * 获得热门城市
	 * 
	 * @param city
	 *            城市
	 * @return
	 */
	public static MResult getHotCity(List<CityTag> cityTags) {
		return ServerService.getHotCity(cityTags);
	}
	/**
	 * 获得热门搜索词
	 * @param words 搜索词集合
	 * @return
	 */
	public static MResult getHotWord(List<String> words) {
		return ServerService.getHotWord(words);
	}

	/**
	 * 获得我的订单列表
	 * 
	 * @param Orders
	 * @param page
	 * @param orderstatus
	 * @return
	 */
	public static MResult getOrders(List<Order> Orders, int page,
			int orderstatus) {
		return ServerService.getOrders(Orders, uid, session, page, orderstatus);
	}

	/**
	 * 获取广告
	 * 
	 * @param orderId
	 * @return
	 */
	public static MResult getAdvertisings(List<Advertising> advertisings,
			String orderId) {
		return ServerService.getAdvertisings(advertisings);
	}

	/**
	 * 取消订单
	 * 
	 * @param orderId
	 * @return
	 */
	public static MResult cancel_order(String orderId) {
		return ServerService.cancel_order(orderId, uid, session);
	}

	/**
	 * 验优惠码证
	 * 
	 * @param code
	 * @param quantity
	 * @param couponId
	 * @return
	 */
	public static MResult confirm_Coupon_Code(String code, String quantity,
			String couponId) {
		return ServerService.confirm_coupon_code(code, quantity, couponId);
	}

	/**
	 * 获得团购详情
	 */
	public static MResult getShopDetail(Shop shop, String coupon_id) {
		return ServerService.getShop_detail(shop, coupon_id);
	}

	/**
	 * 提交反馈
	 * 
	 * @param content
	 * @return
	 */
	public static MResult setPropose(String content) {
		return ServerService.setPropose(content, uid, session);
	}

	/**
	 * 提交日志
	 * 
	 * @param content
	 * @return
	 */
	public static MResult setLog(String content) {
		return ServerService.setLog(content, uid, session);
	}

	/**
	 * 版本检查
	 * 
	 * @return
	 */
	public static MResult checkVersion() {
		return ServerService.checkVersion();
	}

	/**
	 * 获得商圈
	 * 
	 * @return
	 */
	public static MResult setHotPlace(CityTag citytag) {
		if (mLocation != null) {
			lat = String.valueOf(mLocation.getLat());
			lng = String.valueOf(mLocation.getLng());
		}
		if (mLocation != null && "".equals(selectCity)) {// 没有选中城市 ，city为自动获取城市
			city = mLocation.getCity();
		} else {
			city = selectCity;
		}
		return ServerService.setHotPlace(citytag, lat, lng, city);
	}

	/**
	 * 纠偏
	 * 
	 * @param location
	 * @return
	 */
	public static MResult geo_offset(MLocation mLocation,Location location) {
		return ServerService.geo_offset(mLocation,location);
	}

	/**
	 * cps
	 * 
	 * @param cid
	 * @return
	 */
	public static MResult isCps(String cid) {
		return ServerService.isCps(cid);
	}

	/**
	 * 银行卡支付
	 * 
	 * @param location
	 * @return
	 */
	public static String alipay_card_first(String uid, String coupon_id,
			String size, String tel, String receiver_name,
			String receiver_address, String leave_msg, String mCouponCode) {
		return ServerService.alipay_card_first(uid, coupon_id, size, tel,
				receiver_name, receiver_address, leave_msg, mCouponCode);
	}

	/**
	 * 银行卡再次支付
	 * 
	 * @param location
	 * @return
	 */
	public static String alipay_card_again(String uid, String order_id) {
		return ServerService.alipay_card_again(uid, order_id);
	}
}
